package console.manager;

import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Vector;
import console.ConsoleException;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import console.Utilities;
import java.io.StringWriter;
import java.io.PrintWriter;

public class TaskPanel extends JPanel implements PropertyChangeListener {
	class StatusAction extends AbstractAction {
		final Component parent;
		public StatusAction(String str,Icon ico,Component parent) {
			super(str,ico);
			this.parent=parent;
		}
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(parent,traceText,"Task Exception",JOptionPane.ERROR_MESSAGE);
		}
	}
	static Vector<JProgressBar> taskList = new Vector<JProgressBar>();
	final JProgressBar progressBar;
	final JButton statusButton;
	final StatusAction statusAction;
	final ImageIcon doneIcon;
	final ImageIcon errorIcon;
	final ImageIcon runIcon;
	String traceText;
	int traceTaskId;
	public TaskPanel(JFrame rootFrame) {
		doneIcon = Utilities.loadIcon("done.png");
		errorIcon = Utilities.loadIcon("error.png");
		runIcon = Utilities.loadIcon("run.png");
		statusAction = new StatusAction("",doneIcon,rootFrame);
		statusButton = new JButton(statusAction);
		statusAction.setEnabled(false);
		progressBar = new JProgressBar();
		setLayout(new BorderLayout());
		progressBar.setStringPainted(true);
		progressBar.setMinimum(0);
		add(statusButton,BorderLayout.WEST);
		add(progressBar,BorderLayout.CENTER);
		rootFrame.add(this,BorderLayout.SOUTH);
		rootFrame.pack();
		TaskManager.addListener(this);
	}
	public JProgressBar pb() {
		return(progressBar);
	}
	public void refresh(Task task) throws ConsoleException {
		if(task.getProgressTotal()!=0) {
			progressBar.setIndeterminate(false);
			progressBar.setMaximum(task.getProgressTotal());
			progressBar.setValue(task.getProgress());
		} else {
			if(task.getProgress()!=0) {
				progressBar.setIndeterminate(true);
			} else {
				progressBar.setIndeterminate(false);
				progressBar.setValue(task.getProgress());
			}
		}
		int state=task.getState();
		if((state==TaskManager.STATE_DONE)||(state==TaskManager.STATE_ERROR)) {
				progressBar.setIndeterminate(false);
		}
		if(task.getStateInfo()!=null) {
			progressBar.setString("[" + task.name.trim() + "] " + task.getStateInfo());
		} else {
			progressBar.setString(task.name);
		}
		if((task.getState()==TaskManager.STATE_ERROR) ||
		   (task.getState()==TaskManager.STATE_FAIL)) {
				statusButton.setIcon(errorIcon);
				if(task.getException()!=null) {
					statusAction.setEnabled(true);
        	StringWriter sw = new StringWriter();
	        PrintWriter pw = new PrintWriter(sw, true);
	        task.getException().printStackTrace(pw);
	        pw.flush();
	        sw.flush();
	        traceText =  sw.toString();
					traceTaskId = task.getTaskId();
				} else {
					if(task.getParentTaskId()==traceTaskId) {
						traceTaskId=task.getTaskId();
					} else {
						statusAction.setEnabled(false);
					}
				}
		} else if(task.getState()==TaskManager.STATE_DONE){
			statusButton.setIcon(doneIcon);
			statusAction.setEnabled(false);
		} else {
			statusButton.setIcon(runIcon);
			statusAction.setEnabled(false);
		}
	} 
  public void propertyChange(PropertyChangeEvent e) { 
		try {
			refresh((Task)e.getSource());
		} catch (ConsoleException exp) {
		}
	}
}
